package com.zeny.coding.service.impl;


import com.zeny.coding.constants.CourseConstants;
import com.zeny.coding.dto.CourseContentFileDto;
import com.zeny.coding.entity.CourseContentFile;
import com.zeny.coding.entity.CourseContentFileExample;
import com.zeny.coding.mapper.CourseContentFileMapper;
import com.zeny.coding.service.CourseContentFileService;
import com.zeny.coding.util.SnowFlake;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <h2>课程内容文件服务</h2>
 *
 * @Author Zeny
 */
@SuppressWarnings("all")
@Service
public class CourseContentFileServiceImpl implements CourseContentFileService {

    @Autowired
    private CourseContentFileMapper courseContentFileMapper;

    @Override
    public List<CourseContentFileDto> list(String courseId) {
        //设置条件
        CourseContentFileExample courseContentFileExample = new CourseContentFileExample();
        courseContentFileExample.createCriteria().andCourseIdEqualTo(courseId);
        //查询结果
        List<CourseContentFile> courseContentFileList = this.courseContentFileMapper.selectByExample(courseContentFileExample);
        //封装成DTO传输
        List<CourseContentFileDto> courseContentFileDtoList = new ArrayList<>();
        for (CourseContentFile courseContentFile : courseContentFileList) {
            CourseContentFileDto courseContentFileDto = new CourseContentFileDto();
            BeanUtils.copyProperties(courseContentFile, courseContentFileDto);
            courseContentFileDtoList.add(courseContentFileDto);
        }
        return courseContentFileDtoList;
    }

    @Transactional
    @Override
    public void save(CourseContentFileDto courseContentFileDto) {
        //DTO转~
        CourseContentFile courseContentFile = new CourseContentFile();
        BeanUtils.copyProperties(courseContentFileDto, courseContentFile);
        //算法算法生成ID
        SnowFlake snowFlake = new SnowFlake(
                CourseConstants.dataCenterId,
                CourseConstants.machineId);
        courseContentFile.setId(String.valueOf(snowFlake.nextId()));
        this.courseContentFileMapper.insertSelective(courseContentFile);
    }


    @Transactional
    @Override
    public void deleteById(String id) {
        this.courseContentFileMapper.deleteByPrimaryKey(id);
    }

    @Transactional
    @Override
    public void deleteByCourseId(String courseId) {
        CourseContentFileExample courseContentFileExample = new CourseContentFileExample();
        courseContentFileExample.createCriteria().andCourseIdEqualTo(courseId);
        this.courseContentFileMapper.deleteByExample(courseContentFileExample);
    }
}
